/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmpp.client.api.bd;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;

/**
 *
 * @author Miguel Gonzalez - Juanan Mangudo - Telecor
 */
public class BD {

    private final String CADENA_MYSQL = "com.mysql.jdbc.Driver";
    private Connection connection = null;
    
    public BD() throws XMPPExceptionSQL {
        try {
            Class.forName(CADENA_MYSQL); }
        catch (Exception e)
        {
            throw new XMPPExceptionSQL(e);
        } 
    }
    
    public void openConnection(String userName, String passwordName, String host, String bdName) throws XMPPExceptionSQL {
        try {
            connection = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + "/" + bdName,
                    userName, passwordName);
        } catch(SQLException ex) {
            throw new XMPPExceptionSQL(ex);
        }
    }
    
    public Connection getConection() {
        return connection;
    }
    
    public void closeConnection() throws XMPPExceptionSQL {
        try {
            if(connection != null) {
                connection.close();
            }
        } catch(SQLException ex) {
            throw new XMPPExceptionSQL(ex);
        }
    }
    
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
}
